/**
 * Copyright 2009-2011 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.myjung.model;

import java.io.Serializable;
import java.lang.reflect.Field;

import net.myjung.Myjung;
import net.myjung.model.util.IdGenerator;

/**
 * The super class of the data model classes.<br>
 * <br>
 * You can make XML string of subclasses using toString or toXMLString method.<br>
 * Also you can make a new object of subclasses using toObject method.
 * 
 * @author Steve M. Jung
 */
@SuppressWarnings("serial")
public abstract class BaseObject implements Serializable {
	/**
	 * ALL depth datum
	 */
	public static final String LEVEL_ALL = Myjung.LEVEL_ALL;
	/**
	 * Collection type fields will not be wrapped from multiple name XML
	 * elements when you get XML string
	 */
	public static final String LEVEL_REDUCED = Myjung.LEVEL_REDUCED;
	/**
	 * Only primitive type will be considered. (Big datum such as text type data
	 * will also be considered.)
	 */
	public static final String LEVEL_FLAT = Myjung.LEVEL_FLAT;
	/**
	 * Only primitive type will be considered. (Big datum such as text type data
	 * will not be considered, too.)
	 */
	public static final String LEVEL_LITE = Myjung.LEVEL_LITE;

	/**
	 * Gets reflect.Field of this class
	 * 
	 * @param name
	 *            The field name to get
	 * @return
	 */
	public Field field(String name) {
		return Myjung.getField(getClass(), name);
	}
	public Field[] fields() {
		return Myjung.getFields(getClass());
	}
	/**
	 * The same as getParameter function (This is for Groovy)
	 * 
	 * @param name
	 * @return
	 */
	public Object get(String name) {
		return Myjung.getValue(this, name);
	}
	/**
	 * The same as setParamter function (This is for Groovy)
	 * 
	 * @param name
	 * @param value
	 */
	public void set(String name, Object value) {
		Myjung.setValue(this, name, value);
	}
	public Object value(String name) {
		return Myjung.getValue(this, name);
	}
	public Object value(String name, Object value) {
		return Myjung.setValue(this, name, value);
	}

	/**
	 * Makes a new object of this class from the XML string.
	 * 
	 * @param xml
	 *            The XML string
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T extends BaseObject> T toObject(String xml) {
		return (T) toObject(xml, LEVEL_ALL);
	}
	/**
	 * Makes a new object of this class from the XML string.
	 * 
	 * @param xml
	 *            The XML string
	 * @param level
	 *            depth level to populate from the XML data.
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T extends BaseObject> T toObject(String xml, String level) {
		if (Myjung.isEmpty(xml))
			return null;
		return Myjung.populate(xml, (T) this, level);
	}

	@Override
	public String toString() {
		return toString(null, null, "5");
	}
	/**
	 * 
	 * @param level
	 *            The depth level
	 * @return
	 */
	public String toString(String level) {
		return toString(null, null, level);
	}
	/**
	 * 
	 * @param name
	 *            The root element name
	 * @param tab
	 *            The tab string to append as a prefix
	 * @param level
	 *            The depth level
	 * @return
	 */
	public String toString(String name, String tab, String level) {
		return Myjung.toXMLString(this, false, name, tab, level);
	}
	/**
	 * Makes an XML string of this object.
	 * 
	 * @return
	 */
	public String toXMLString() {
		return toXMLString(LEVEL_ALL);
	}
	/**
	 * 
	 * @param level
	 *            The depth level
	 * @return
	 */
	public String toXMLString(String level) {
		return Myjung.toXMLString(this, true, null, null, level);
	}
	@Override
	public Object clone() throws CloneNotSupportedException {
		try {
			return Myjung.populate(this, getClass().newInstance());
		} catch (Exception e) {
			return super.clone();
		}
	}
	@Override
	public int hashCode() {
		try {
			String id = IdGenerator.getId(this);
			if (id == null)
				return super.hashCode();
			int len = id.length();
			String hash = id.substring(len - 6, len);
			return Integer.parseInt(hash, 16);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return super.hashCode();
	}
	@Override
	public boolean equals(Object obj) {
		if (super.equals(obj))
			return true;
		if (obj == null || !(obj instanceof BaseObject) || this.getClass() != obj.getClass())
			return false;
		String id = IdGenerator.getId(this);
		if (id == null)
			return super.equals(obj);
		return id.equals(IdGenerator.getId((BaseObject) obj));
	}

}